﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HyEduOnline.Server.Admin;
using HyEduOnline.Entities;
using HyEduOnline.Data;
using HyEduOnline.Models;
using HyEduOnline.Server.Common;

namespace HyEduOnline.Controllers
{
    public class LoginController : Controller
    {
        //
        // GET: /Login/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult SysUser()
        {
            return View();
        }

        /// <summary>
        /// 用户登录界面
        /// </summary>
        /// <returns></returns>
        public ActionResult CommonUser()
        {
            if (frontCurrentTea.GetUserID() < 1 || frontCurrentStu.GetUserID() < 1)
            {
                if (Request.Cookies.AllKeys.Contains("User"))
                {
                    string name = Request.Cookies["User"]["LoginName"];
                    string pass = Request.Cookies["User"]["LoginPassWord"];
                    string passWord = CommonHelper.XOR(pass);//解密密码
                    string userType = Request.Cookies["User"]["UserType"];
                    int type = int.Parse(userType);
                    if (!String.IsNullOrEmpty(name) && !String.IsNullOrEmpty(passWord))
                    {
                        passWord = Utitil.MD5(passWord);//MD5加密
                        if (type == 1)//教师
                        {
                            if (LoginValidate.ValidateTeacher(name, passWord))//验证
                            {
                                return RedirectToAction("Index", "Teacher");
                            }
                        }
                        else if (type == 0)//学生
                        {
                            if (LoginValidate.ValidateStudent(name, passWord))//验证
                            {
                                return RedirectToAction("Index", "Student");
                            }
                        }
                    }
                }
            }

            return View();
        }
    }
}
